package com.jg.visitor.controller.common;

import com.jg.visitor.common.bean.ResponseBean;
import com.jg.visitor.controller.dto.FileUploadRecordDTO;
import com.jg.visitor.service.FileUploadRecordService;
import com.jg.visitor.service.GlobalConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Controller
@Api(tags = "文件上传接口")
@RequestMapping("/api/upload")
@Slf4j
public class UploadController {

    @Resource
    private FileUploadRecordService fileUploadRecordService;
    @Resource
    private GlobalConfigService globalConfigService;

    @ApiOperation("上传图片")
    @PostMapping(value = "/add-image",consumes = "multipart/*",headers = "content-type=multipart/form-data")
    @ResponseBody
    public ResponseBean imageUpload(@RequestPart("file") MultipartFile file){
        try {
            FileUploadRecordDTO fileUploadRecordDTO = fileUploadRecordService.uploadImg(file);
            return ResponseBean.success(fileUploadRecordDTO);
        } catch (IOException e) {
            log.error("upload error...",e);
            return ResponseBean.error(-1,"图片上传发生未知错误！");
        }

    }

    @ApiOperation("下载批量导入用户数据模板")
    @GetMapping("/download-batch-add-template")
    public void downloadTemplate(){
        globalConfigService.downloadTemplate();
    }
}
